import st from "./showtoast.js";

export default {
	pid: "", //上级ID
	/**
	 * 选择图片
	 */
	onChooseImage(count, callBack) {
		uni.chooseImage({
			count: count,
			success: (res) => {
				if (callBack) {
					callBack(res.tempFilePaths);
				}
			},
		});
	},
	/**
	 * 根据图片的字符串，返回图片的数组
	 */
	getImageArray(images) {
		if (images === undefined || images === null || images === "") {
			return [];
		} else {
			return images.split(",");
		}
	},
	
	/**
	 * 打开地图获取地址之后取省市区街道
	 */
	onHandleAddress(address) {
		var reg = "(?<province>[^省]+省|.+自治区|[^澳门]+澳门|北京|重庆|上海|天津|台湾|[^香港]+香港|[^市]+市)?(?<city>[^自治州]+自治州|[^特别行政区]+特别行政区|[^市]+市|.*?地区|.*?行政单位|.+盟|市辖区|[^县]+县)(?<county>[^县]+县|[^市]+市|[^镇]+镇|[^区]+区|[^乡]+乡|.+场|.+旗|.+海域|.+岛)?(?<address>.*)"
		var location = address.match(reg);
		
		if (location !== undefined && location !== null) {
			var groups = location.groups;
			var province = groups.province === undefined ? groups.city : groups.province;
			var city = groups.city;
			var district = groups.county;
			var street = groups.address;
			return [province, city, district, street];
		} else {
			return [];
		}
	},
	
	/**
	 * 判断表单列表正则
	 */
	judgeFormData(list, dataInfo) {
		for (let i = 0; i < list.length; i++) {
			const item = list[i];
			const value = dataInfo[item.key];
	
			var isMust = true;
			if (item.require !== undefined) {
				isMust = item.require;
			}
			var isShow = true;
			if (item.isShow !== undefined) {
				isShow = item.isShow;
			}
			
			if (isMust && isShow && (value === null || (typeof value === 'string' && value === "") || (typeof value === "object") && value.length === 0)) {
				st.show(item.tips);
				return false;
			}
		}
		
		return true;
	},
	
	/**
	 * 将时间戳转换成时间
	 */
	onTimeStringChangeDate(timeString) {
		var timeSpe = +timeString * 1000;
		
		var date = new Date(timeSpe);
		
		var year = `${date.getFullYear()}`;
		var month = `${date.getMonth() + 1}`.padStart(2, "0");
		var day = `${date.getDate()}`.padStart(2, "0");
		
		var hour = `${date.getHours()}`.padStart(2, "0");
		var min = `${date.getMinutes()}`.padStart(2, "0");
		var second = `${date.getSeconds()}`.padStart(2, "0");
		
		return `${year}-${month}-${day} ${hour}:${min}:${second}`;
	},
	
	/**
	 * 保存图片
	 */
	onSaveImage(imgURL, callBack) {
		uni.getImageInfo({
			src: imgURL,
			success: res => {
				uni.saveImageToPhotosAlbum({
					filePath: res.path,
					success: () => {
						st.success("保存成功", () => {
							if (callBack) {
								callBack();
							}
						});
					},
					fail: () => {
						st.show("保存失败");
					}
				})
			}
		})
	},
}